/*
 * DirectoryWatcher.java July 2009
 *
 * Copyright (C) 2009, Quirino Zagarese <quirino@laszloitalia.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 */
package org.eu4ria.core.hotdeploy;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class DirectoryWatcher extends AbstractResourceWatcher {

	private Map<String, Long> currentFiles = new HashMap<String, Long>();

	private String directory;

	private Map<String, Long> prevFiles = new HashMap<String, Long>();


	public DirectoryWatcher(String directoryPath, int intervalSeconds)
			throws IllegalArgumentException {


		super(intervalSeconds, directoryPath + " interval watcher.");


		File theDirectory = new File(directoryPath);

		if (theDirectory != null && !theDirectory.isDirectory()) {


			String message = "The path " + directory
					+ " does not represent a valid directory.";
			throw new IllegalArgumentException(message);

		}


		this.directory = directoryPath;

	}

	public void start() {

		takeSnapshot();

		super.start();

		File theDirectory = new File(directory);
		monitoringStarted(theDirectory);
	}

	public void stop() {

		super.stop();

		File theDirectory = new File(directory);
		monitoringStopped(theDirectory);
	}


	private void takeSnapshot() {

		prevFiles.clear();
		prevFiles.putAll(currentFiles);

		currentFiles.clear();

		File theDirectory = new File(directory);
		File[] children = theDirectory.listFiles();


		for (int i = 0; i < children.length; i++) {

			File file = children[i];
			currentFiles.put(file.getAbsolutePath(), new Long(file
					.lastModified()));

		}

	}


	protected void doInterval() {


		takeSnapshot();
		Iterator currentIt = currentFiles.keySet().iterator();

		while (currentIt.hasNext()) {

			String fileName = (String) currentIt.next();
			Long lastModified = (Long) currentFiles.get(fileName);

			if (!prevFiles.containsKey(fileName)) {
				resourceAdded(fileName);
			}

			else if (prevFiles.containsKey(fileName)) {

				Long prevModified = (Long) prevFiles.get(fileName);

				if (prevModified.compareTo(lastModified) != 0) {
					resourceChanged(new File(fileName));
				}
			}
		}

		Iterator prevIt = prevFiles.keySet().iterator();

		while (prevIt.hasNext()) {

			String fileName = (String) prevIt.next();

			if (!currentFiles.containsKey(fileName)) {
				resourceDeleted(new File(fileName));
			}
		}
	}
}
